package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.model.SubmitGoods;
import com.zw.yzk.kabu.utils.ResUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/3/12.
 */

public class ConfirmGoodsOrderGoodsAdapter extends RecyclerView.Adapter<ConfirmGoodsOrderGoodsAdapter.Holder> {

    private Context context;
    private List<SubmitGoods> dataList;

    public ConfirmGoodsOrderGoodsAdapter(List<SubmitGoods> dataList) {
        this.dataList = new ArrayList<>();
        if (dataList != null) {
            this.dataList.addAll(dataList);
        }
    }

    public void refresh(List<SubmitGoods> dataList) {
        this.dataList.clear();
        if (dataList != null) {
            this.dataList.addAll(dataList);
        }
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_confrm_goods_order_goods_list, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        SubmitGoods goods = dataList.get(position);
        holder.name.setText(goods.getName());
        holder.property.setText(goods.getProperty());
        holder.count.setText(context.getResources().getString(R.string.goods_count, goods.getCount()));
        //TODO 设置运费
        holder.price.setText(ResUtils.formatCurrency(context, goods.getPrice()));

        Glide.with(context)
                .load(goods.getImage())
                .error(R.drawable.im_default)
                .into(holder.image);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.image)
        ImageView image;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.type)
        TextView property;
        @Bind(R.id.count)
        TextView count;
        @Bind(R.id.freight)
        TextView freight;
        @Bind(R.id.price)
        TextView price;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
